package com.company.manage.system.utils;

import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.apache.shiro.web.util.WebUtils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * Created by HuangYong on 2016/1/23.
 * desc:扩展认证默认过滤
 */
public class FormAuthenticationCaptchaFilter extends FormAuthenticationFilter {
    private static final String TAG = "FormAuthenticationCaptchaFilter";

    public static final String DEFAULT_CAPTCHA_PARAM = "captcha";
    private String captchaParam = DEFAULT_CAPTCHA_PARAM;

    public String getCaptchaParam() {
        return captchaParam;
    }

    public String getCaptcha(ServletRequest request) {
        return WebUtils.getCleanParam(request, getCaptchaParam());
    }

    protected AuthenticationToken createToken(ServletRequest request, ServletResponse response) {
        String username = getUsername(request);
        String password = getPassword(request);
        String captcha = getCaptcha(request);
        boolean rememberMe = isRememberMe(request);
        String host = getHost(request);
        UsernamePasswordCaptchaToken captchaToken = new UsernamePasswordCaptchaToken(username, password.toCharArray(), rememberMe, host, captcha);
        return captchaToken;
    }
} 